package service

import (
	"errors"

	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"

	"go-admin/app/admin/models"
	"go-admin/app/admin/service/dto"
	"go-admin/common/actions"
	cDto "go-admin/common/dto"
)

type Advertisements struct {
	service.Service
}

// GetPage 获取Advertisements列表
func (e *Advertisements) GetPage(c *dto.AdvertisementsGetPageReq, p *actions.DataPermission, list *[]models.Advertisements, count *int64) error {
	var err error
	var data models.Advertisements

	query := e.Orm.Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).
		Order("created_at DESC")

	//处理关键词搜索
	if c.Keyword != "" {
		query = query.Where("title LIKE ? OR description LIKE ?", "%"+c.Keyword+"%", "%"+c.Keyword+"%")
	}

	// 处理创建人
	if c.CreateBy != 0 {
		query = query.Where("create_by = ?", c.CreateBy)
	}

	// 处理位置
	if c.Position != "" {
		query = query.Where("position = ?", c.Position)
	}

	err = query.Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("AdvertisementsService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取Advertisements对象
func (e *Advertisements) Get(d *dto.AdvertisementsGetReq, p *actions.DataPermission, model *models.Advertisements) error {
	var data models.Advertisements

	err := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetAdvertisements error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Insert 创建Advertisements对象
func (e *Advertisements) Insert(c *dto.AdvertisementsInsertReq) error {
	var err error
	var data models.Advertisements
	c.Generate(&data)
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("AdvertisementsService Insert error:%s \r\n", err)
		return err
	}
	return nil
}

// Update 修改Advertisements对象
func (e *Advertisements) Update(c *dto.AdvertisementsUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.Advertisements{}
	e.Orm.Scopes(
		actions.Permission(data.TableName(), p),
	).First(&data, c.GetId())
	c.Generate(&data)

	db := e.Orm.Save(&data)
	if err = db.Error; err != nil {
		e.Log.Errorf("AdvertisementsService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	return nil
}

// IncrementClicks 增加广告点击次数
func (e *Advertisements) IncrementClicks(id int64) error {
	err := e.Orm.Model(&models.Advertisements{}).
		Where("id = ?", id).
		UpdateColumn("clicks", gorm.Expr("clicks + ?", 1)).Error
	if err != nil {
		e.Log.Errorf("AdvertisementsService IncrementClicks error:%s \n", err)
		return err
	}
	return nil
}

// DecrementClicks 减少广告点击次数
func (e *Advertisements) DecrementClicks(id int64) error {
	err := e.Orm.Model(&models.Advertisements{}).
		Where("id = ? AND clicks > 0", id).
		UpdateColumn("clicks", gorm.Expr("clicks - ?", 1)).Error
	if err != nil {
		e.Log.Errorf("AdvertisementsService DecrementClicks error:%s \n", err)
		return err
	}
	return nil
}

// IncrementImpressions 增加广告展示次数
func (e *Advertisements) IncrementImpressions(id int64) error {
	err := e.Orm.Model(&models.Advertisements{}).
		Where("id = ?", id).
		UpdateColumn("impressions", gorm.Expr("impressions + ?", 1)).Error
	if err != nil {
		e.Log.Errorf("AdvertisementsService IncrementImpressions error:%s \n", err)
		return err
	}
	return nil
}

// DecrementImpressions 减少广告展示次数
func (e *Advertisements) DecrementImpressions(id int64) error {
	err := e.Orm.Model(&models.Advertisements{}).
		Where("id = ? AND impressions > 0", id).
		UpdateColumn("impressions", gorm.Expr("impressions - ?", 1)).Error
	if err != nil {
		e.Log.Errorf("AdvertisementsService DecrementImpressions error:%s \n", err)
		return err
	}
	return nil
}

// Remove 删除Advertisements
func (e *Advertisements) Remove(d *dto.AdvertisementsDeleteReq, p *actions.DataPermission) error {
	var data models.Advertisements

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Delete(&data, d.GetId())
	if err := db.Error; err != nil {
		e.Log.Errorf("Service RemoveAdvertisements error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}
